﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI.WebControls;
using Hematology.Data;
using Hematology.HmtMembership;
using Hematology.Tools;
using Hematology.UserSite.Controls;

namespace Hematology.UserSite
{
	public partial class Projects : NgoPage
	{
		private void FillInfoList(Repeater targetRepeater)
		{
			List<SiteContent> lProjectsInfo = SiteContent.ListAllContent((int)EnumHelper.ContentType.Projects, !MembershipHelper.MembershipService.IsAuthorizedRequest, true);

			targetRepeater.DataSource = lProjectsInfo;
			targetRepeater.DataBind();
		}

		private void FillInfo()
		{
			string sSiteContentId = Request.QueryString["id"];
			int iSiteContentId;
			if (!String.IsNullOrEmpty(sSiteContentId) && Int32.TryParse(sSiteContentId, out iSiteContentId))
			{
				ptTitle.NavigateUrl = "/Projects.aspx";

				SiteContent oSiteContent = SiteContent.Get(iSiteContentId);
				if (oSiteContent != null)
				{
					List<SiteContent> oSiteContentList = new List<SiteContent>
                                                                 {
                                                                     oSiteContent
                                                                 };

					fvProjects.DataSource = oSiteContentList;
					fvProjects.DataBind();
				}
			}
			else
			{
				FillInfoList(rptProjectsFull);
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				FillInfoList(rptProjects);
				FillInfo();
			}
		}

		protected void rptContentAttach_ItemCommand(object source, RepeaterCommandEventArgs e)
		{
			if (e.CommandName == "OpenAttach")
			{
				int iAttachId = Convert.ToInt32(e.CommandArgument);
				FileAttach fileAttach = FileAttach.Get(iAttachId);
				if (fileAttach != null)
				{
					Response.Redirect(Path.Combine(ConfigReader.SiteContentAttachmentPath, fileAttach.DocumentName));
				}
			}
		}
	}
}